import tool from '../modules/tool'
const controls = tool.cControls
const controls_settings = controls.settings
import { useStore } from '../modules/store'
const store = useStore()

export default async (reloadPage, handleError) => {

    const status = {
        data: {},
        async get(loading = true) {
            let re = await tool.requestData('/question/AIModify/getStatus', {}, {}, 'get', loading)
            if (re.status) {
                this.data = re.data
                this.trySetAuto()
            } else {
                throw re.content
            }
        },
        autoRefreshTimer: null,
        trySetAuto() {
            if (this.autoRefreshTimer != null) return
            status.autoRefreshTimer = setInterval(async () => {
                await status.get(false)
                reloadPage()
                if (status.data.running) {

                } else {
                    clearInterval(status.autoRefreshTimer)
                    this.autoRefreshTimer = null
                }
            }, 5000);
        },
    }

    await status.get()

    const refresh = async () => {
        return {
            type: 1,
            main: {
                tab: [
                    {
                        title: '',
                        bottomControls: [],
                        topControls: [
                            controls.button({
                                title: '刷新',
                                leftIcon: 'refresh',
                                type: 'primary',
                                async callBack() {
                                    await tool.flow(async () => {
                                        await status.get()
                                        reloadPage()
                                    }, err => {
                                        tool.toast.error(`${err}`)
                                    })
                                }
                            }),
                        ],
                        data: {
                            type: 'settings',
                            list: [
                                controls_settings.buttonGroup('状态信息', [
                                    controls_settings.button('状态', () => {

                                    }, 'text', status.data.running ? '正在进行' : '未进行'),
                                    ...(() => {
                                        if (status.data.running) {
                                            return [
                                                controls_settings.button('当前步骤', () => {

                                                }, 'text', status.data.currentType === 1 ? '给纯文字题目添加关键词' : '给带图片题目添加关键词'),
                                                controls_settings.button('已完成比例', () => {

                                                }, 'text', `${status.data.finishedPer}%`),
                                                controls_settings.button('已用时', () => {

                                                }, 'text', status.data.usedTime),
                                                controls_settings.button('预测剩余时间', () => {

                                                }, 'text', status.data.lastTime),
                                                controls_settings.button('未完善的纯文字题目数量', () => {

                                                }, 'text', status.data.total1),
                                                controls_settings.button('未完善的带图片题目数量', () => {

                                                }, 'text', status.data.total2),
                                                controls_settings.button('未完善的题目数量', () => {

                                                }, 'text', status.data.total),
                                                controls_settings.button('本次操作已完成完善的题目数量', () => {

                                                }, 'text', status.data.finished),
                                            ]
                                        }
                                        return [
                                            controls_settings.button('数据库中未完善的题目数量', () => {

                                            }, 'text', status.data.unmodified),
                                        ]
                                    })(),
                                ]),
                                controls_settings.buttonGroup('操作', [
                                    ...(() => {
                                        if (status.data.running) {
                                            return []
                                        }
                                        return [
                                            controls_settings.button('开始', () => {
                                                store.showModal2('提示', '是否开始完善题目？', () => {
                                                    tool.flow(async () => {
                                                        let re = await tool.requestData('/question/AIModify/start', {}, {}, 'post')
                                                        await tool.wait(1)
                                                        store.hideModal2()
                                                        if (re.status) {
                                                            tool.toast.success('系统已开始使用AI对未完善的题目进行完善。')
                                                            setTimeout(() => {
                                                                status.get(false)
                                                            }, 500);
                                                        } else {
                                                            throw re.content
                                                        }
                                                    }, err => tool.toast.error(`${err}`))
                                                }, true, () => {
                                                    store.hideModal2()
                                                })
                                            }),
                                            controls_settings.button('将全部题目的已完善状态和AI生成内容去除', () => {
                                                store.showModal2('提示', '是否清除全部题目的已完善状态和AI生成内容？', () => {
                                                    tool.flow(async () => {
                                                        let re = await tool.requestData('/question/AIModify/reset', {}, {}, 'post')
                                                        store.hideModal2()
                                                        if (re.status) {
                                                            tool.toast.success('完成')
                                                        } else {
                                                            throw re.content
                                                        }
                                                    }, err => tool.toast.error(`${err}`))
                                                }, true, () => {
                                                    store.hideModal2()
                                                })
                                            }),
                                        ]
                                    })(),
                                ]),
                            ],
                            callBack: async (e) => {
                                console.log(e)
                            },
                        }
                    }
                ]
            }
        }
    }

    return refresh
}